package com.chart.web.controller.chart;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chart.chart.domian.ChartOrders;
import com.chart.chart.service.IChartOrdersService;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.SecurityUtils;
import com.chart.web.controller.chart.vo.UserOrderVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 此处写该类描述
 *
 * @author 罗继全
 * 2024/9/11 13:46
 */

@RestController
@RequestMapping("/chart/orders")
public class ChartOrdersController {


    @Autowired
    private IChartOrdersService chartOrdersService;


    /**
     * 分页查询订单详情
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping("list")
    public HashMap<String, Object> queryOrder(int pageNum, int pageSize) {
        if (pageNum <= 0 || pageSize <= 0) {
            HashMap<String, Object> response = new HashMap<>();
            response.put("msg", "pageNum或pageSize需要大于0");
            response.put("code", 400);
            return response;
        }
        // 将分页参数传递给 service
        List<ChartOrders> list = chartOrdersService.listOrdersSortedByCreateTime(pageNum, pageSize);

        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<ChartOrders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ChartOrders::getUserId, userId);

        List<ChartOrders> lists = chartOrdersService.list(lqw);
        int total = lists.size();

        HashMap<String, Object> map = new HashMap<>();
        map.put("msg", "操作成功");
        map.put("code", 200);
        map.put("data", list);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("total", total);
        return map;
    }











    @GetMapping(value = "/search")
    public AjaxResult findPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "") String phonenumber) {

        // 调用 Service 层的分页查询方法
        PageInfo<ChartOrders> pageInfo = chartOrdersService.findPage(page, size, phonenumber);

        // 构建返回结果
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg", "操作成功");
        map.put("code", 200);
        map.put("data", pageInfo.getList()); // 获取分页中的数据列表
        map.put("total", pageInfo.getTotal()); // 总记录数
        map.put("pageNum", pageInfo.getPageNum()); // 当前页码
        map.put("pageSize", pageInfo.getPageSize()); // 每页大小
        return AjaxResult.success(map);  // 使用 AjaxResult 返回
    }


    @GetMapping("queryOrder")
    private AjaxResult queryOrder() {
        List<ChartOrders> chartOrdersList = chartOrdersService.queryOrder();
        for (ChartOrders chartOrders : chartOrdersList) {
            Integer invoice = chartOrders.getInvoice();
            if (invoice == null || invoice == 0) {
                chartOrders.setInvoiceStatus("未开票");
            } else if (invoice == 1) {
                chartOrders.setInvoiceStatus("已开票");
            }
        }

        return AjaxResult.success(chartOrdersList);
    }


   @GetMapping("/queryOrder/exportExcel")
    public ResponseEntity<byte[]> exportOrders() {
        try {
            return chartOrdersService.exportOrdersToExcel();
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }







}
